package com.ms.service.module.analysisgroup;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.gmtool.message.SGmtoolKickAllConnectionNotify;
import com.ms.game.itembox.AccountItemChangeWay;
import com.ms.game.itembox.ItemChangeWay;
import com.ms.service.module.analysis.GameNameWithHallType;
import com.ms.service.module.analysisgroup.message.SCheckSendGiftIntervalTimeData;
import com.ms.service.module.analysisgroup.message.SCoinExpenditureData;
import com.ms.service.module.analysisgroup.message.SGameRnERequestData;
import com.ms.service.module.analysisgroup.message.SItemChangeData;
import com.ms.service.module.analysisgroup.message.SPlayerRnERequestData;
import com.ms.service.module.analysisgroup.message.SUsedCoinCardDetailData;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.common.BaseMessageAction;
import com.xpec.c4.service.module.BasicServiceModule;

public class AnalysisGroupAction extends BaseMessageAction{
	
	/** log object */
	private static final Log log = LogFactory.getLog(AnalysisGroupAction.class);
	
	/**
	 * 取得AnalysisGroupProcessor 
	 */
	private AnalysisGroupProcessor getProc(){
		BasicServiceModule module = getModule();
		if(!(module instanceof AnalysisGroupModule)){
			if(log.isErrorEnabled()){
				log.error(GameUtility.getCurMethodName() + " : get analysis group module type is invalid");
			}
			return null;
		}
		
		AnalysisGroupModule analysisGroupModule = (AnalysisGroupModule) module;
		return analysisGroupModule.getProc();
	}
	
	@MessageType(typeCode = MessageTypes.S_GMTOOL_KICK_ALL_CONNECTION_NOTIFY)
	public void recvKickAllConnectionNotify(Object object) {
		BasicServiceModule module = getModule();
		if (module != null) {
			SGmtoolKickAllConnectionNotify request = (SGmtoolKickAllConnectionNotify) object;
			module.onGameRecvKickAllConnectionNotify(request);
		}
		
		getProc().SaveAllGameRevenueAndExpenditureLog();
		getProc().SaveAllCoinExpenditureLog();
	}
	
	@MessageType(typeCode = MessageTypes.S_ANALYSIS_GAME_REVENUE_AND_EXPENDITURE)
	public void OnRecieveGameRevenueAndExpenditureRequest(SGameRnERequestData data){
		
		GameNameWithHallType gameNameWithHallType = GameNameWithHallType.getENUMbyString(data.getGameName(), data.getHallType());
		if(gameNameWithHallType == null)
			return;
		else{
			//遊戲館收入
			if(data.isRevenue()){
				getProc().addGameRevenue(gameNameWithHallType, data.getChangeValue(), data.isFormal(), data.getOrderID(), data.isCardBet());
			}
			//遊戲館支出
			else{
				String payWay = "";
				if(data.isJP()) payWay = ExpenditurePayWay.JP_RELEASE;
				else payWay = ExpenditurePayWay.PLAYER_SCORE;
				
				getProc().addGameExpenditure(gameNameWithHallType, data.getChangeValue(), payWay, data.isFormal(), data.getOrderID());
			}
		}
	}
	
	@MessageType(typeCode = MessageTypes.S_ANALYSIS_PLAYER_REVENUE_AND_EXPENDITURE)
	public void OnRecievePlayerRevenueAndExpenditureRequest(SPlayerRnERequestData data){
		//玩家收入
		if(data.isRevenue()){
			getProc().addPlayerRevenue(data.getPlayerGUID(), data.getChangeValue(), data.isJP());
		}
		//玩家支出
		else{
			getProc().addPlayerExpenditure(data.getPlayerGUID(), data.getChangeValue());
		}
	}

	@MessageType(typeCode = MessageTypes.S_ANALYSIS_ADD_COIN_EXPENDITURE)
	public void addCoinExpenditure(SCoinExpenditureData data){
		getProc().addCoinExpenditure(data);
	}
	
	@MessageType(typeCode = MessageTypes.S_GMTOOL_UPDATE_COIN_EXPENDITURE_ALERT_VALUE)
	public void updateCoinExpenditureAlertValue(Object object){
		getProc().loadAllCoinExpenditureSetting(false);
	}
	
	@MessageType(typeCode = MessageTypes.S_ANALYSIS_CHECK_SEND_GIFT_INTERVAL_TIME)
	public void checkPlayerSendGiftIntervalTime(SCheckSendGiftIntervalTimeData data){
		getProc().checkPlayerSendGiftIntervalTime(data);
	}
	
	@MessageType(typeCode = MessageTypes.S_ANALYSIS_ITEM_CHANGE)
	public void revItemChange(SItemChangeData data){
		AccountItemChangeWay accountItemChangeWay = null;
		ItemChangeWay itemChangeWay = null;
		
		try {
			if(data.isAccountItemChange()){
				accountItemChangeWay = AccountItemChangeWay.valueOf(data.getItemChangeWay());
			}
			else{
				itemChangeWay = ItemChangeWay.valueOf(data.getItemChangeWay());
			}
			
			getProc().revItemChange(data.isAccountItemChange(), accountItemChangeWay, itemChangeWay, data.getChangeItems());
			
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error("revItemChange can't recognize itemChangeWay: "+data.getItemChangeWay());
			}
			return;
		}
	}
	
	@MessageType(typeCode = MessageTypes.S_ANALYSIS_USED_COIN_CARD_DETAIL)
	public void revUsedCoinCardDetail(SUsedCoinCardDetailData data){
		getProc().revUsedCoinCardDetail(data);
	}
}
